<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>列表的嵌套 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter05.html" />
    
    
    <link rel="prev" href="./chapter03.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="4"
        data-chapter-title="列表的嵌套"
        data-filepath="chapter04.md"
        data-basepath="."
        data-revision="Fri Feb 07 2020 10:06:55 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        列表的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        列表的增删改查
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        列表的遍历
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        列表的嵌套
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        列表推导式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        列表的复制
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="1-&#x5217;&#x8868;&#x5D4C;&#x5957;">1. &#x5217;&#x8868;&#x5D4C;&#x5957;</h2>
<p>&#x7C7B;&#x4F3C;while&#x5FAA;&#x73AF;&#x7684;&#x5D4C;&#x5957;&#xFF0C;&#x5217;&#x8868;&#x4E5F;&#x662F;&#x652F;&#x6301;&#x5D4C;&#x5957;&#x7684;</p>
<p>&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x53C8;&#x662F;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x5C31;&#x662F;&#x5217;&#x8868;&#x7684;&#x5D4C;&#x5957;</p>
<p>&#x6B64;&#x5904;&#x91CD;&#x70B9;&#x638C;&#x63E1;&#x600E;&#x4E48;&#x64CD;&#x4F5C;&#x88AB;&#x5D4C;&#x5957;&#x7684;&#x5217;&#x8868;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>schoolNames = [
<span class="hljs-prompt">... </span>[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>],
<span class="hljs-prompt">... </span>[<span class="hljs-number">11</span>, <span class="hljs-number">22</span>, <span class="hljs-number">33</span>],
<span class="hljs-prompt">... </span>[<span class="hljs-number">111</span>, <span class="hljs-number">222</span>, <span class="hljs-number">333</span>]
<span class="hljs-prompt">... </span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>schoolNames[<span class="hljs-number">1</span>][<span class="hljs-number">2</span>]  <span class="hljs-comment"># &#x83B7;&#x53D6;&#x6570;&#x5B57; 33</span>
<span class="hljs-number">33</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>schoolNames[<span class="hljs-number">1</span>][<span class="hljs-number">2</span>] = <span class="hljs-string">&apos;abc&apos;</span>  <span class="hljs-comment"># &#x628A; 33 &#x4FEE;&#x6539;&#x4E3A; &apos;abc&apos;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>schoolNames
[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">11</span>, <span class="hljs-number">22</span>, <span class="hljs-string">&apos;abc&apos;</span>], [<span class="hljs-number">111</span>, <span class="hljs-number">222</span>, <span class="hljs-number">333</span>]]
<span class="hljs-prompt">&gt;&gt;&gt; </span>schoolNames[<span class="hljs-number">1</span>][<span class="hljs-number">2</span>][<span class="hljs-number">2</span>]  <span class="hljs-comment"># &#x83B7;&#x53D6; &apos;abc&apos; &#x91CC;&#x7684;&#x5B57;&#x7B26;c</span>
<span class="hljs-string">&apos;c&apos;</span>
</code></pre>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;<strong>&#x64CD;&#x4F5C;&#x5D4C;&#x5957;&#x5217;&#x8868;&#xFF0C;&#x53EA;&#x8981;&#x628A;&#x8981;&#x64CD;&#x4F5C;&#x5143;&#x7D20;&#x7684;&#x4E0B;&#x6807;&#x5F53;&#x4F5C;&#x53D8;&#x91CF;&#x540D;&#x6765;&#x4F7F;&#x7528;&#x5373;&#x53EF;&#x3002;</strong></p>
<h2 id="2-&#x5E94;&#x7528;">2. &#x5E94;&#x7528;</h2>
<p>&#x4E00;&#x4E2A;&#x5B66;&#x6821;&#xFF0C;&#x6709;3&#x4E2A;&#x529E;&#x516C;&#x5BA4;&#xFF0C;&#x73B0;&#x5728;&#x6709;8&#x4F4D;&#x8001;&#x5E08;&#x7B49;&#x5F85;&#x5DE5;&#x4F4D;&#x7684;&#x5206;&#x914D;&#xFF0C;&#x8BF7;&#x7F16;&#x5199;&#x7A0B;&#x5E8F;&#xFF0C;&#x5B8C;&#x6210;&#x968F;&#x673A;&#x7684;&#x5206;&#x914D;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> random

<span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x7528;&#x6765;&#x4FDD;&#x5B58;3&#x4E2A;&#x529E;&#x516C;&#x5BA4;</span>
offices = [[],[],[]]

<span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x7528;&#x6765;&#x5B58;&#x50A8;8&#x4F4D;&#x8001;&#x5E08;&#x7684;&#x540D;&#x5B57;</span>
names = [<span class="hljs-string">&apos;A&apos;</span>,<span class="hljs-string">&apos;B&apos;</span>,<span class="hljs-string">&apos;C&apos;</span>,<span class="hljs-string">&apos;D&apos;</span>,<span class="hljs-string">&apos;E&apos;</span>,<span class="hljs-string">&apos;F&apos;</span>,<span class="hljs-string">&apos;G&apos;</span>,<span class="hljs-string">&apos;H&apos;</span>]

i = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> names:
    index = random.randint(<span class="hljs-number">0</span>,<span class="hljs-number">2</span>)    
    offices[index].append(name)

i = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> tempNames <span class="hljs-keyword">in</span> offices:
    print(<span class="hljs-string">&apos;&#x529E;&#x516C;&#x5BA4;%d&#x7684;&#x4EBA;&#x6570;&#x4E3A;:%d&apos;</span>%(i,len(tempNames)))
    i+=<span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> tempNames:
        print(<span class="hljs-string">&quot;%s&quot;</span>%name,end=<span class="hljs-string">&apos;&apos;</span>)
    print(<span class="hljs-string">&quot;\n&quot;</span>)
    print(<span class="hljs-string">&quot;-&quot;</span>*<span class="hljs-number">20</span>)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x4E0B;:</p>
<p><img src="imgs/&#x5217;&#x8868;&#x7684;&#x5D4C;&#x5957;.png" alt=""></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter03.html" class="navigation navigation-prev " aria-label="Previous page: 列表的遍历"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter05.html" class="navigation navigation-next " aria-label="Next page: 列表推导式"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
